package com.rtx.edi.platform.express.auth.controller;

import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.express.auth.dto.PlAuthAppDto;
import com.rtx.edi.model.express.auth.po.PlAuthApp;
import com.rtx.edi.model.express.auth.po.PlAuthShopApp;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.express.auth.service.PlAuthAppService;
import com.rtx.edi.platform.express.auth.service.PlAuthShopAppService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = {"应用信息"})
@RestController
@RequestMapping("/v1/plAuthApp")
public class PlAuthAppController {
	
	@Autowired
	private PlAuthAppService  plAuthAppService;
	
	@Autowired
	private PlAuthShopAppService plAuthShopAppService;
	
	@ApiOperation(value = "应用信息列表", notes = "分页获取应用信息列表")
    @PostMapping("qryPlAuthAppList")
	public EdiResponse qryPlAuthApp(@RequestBody PlAuthAppDto dto) {
		PlAuthApp app =new PlAuthApp();
		BeanUtils.copyProperties(dto,app);
		Page<PlAuthApp>  pageParam = new Page<>(dto.getPageNum(),dto.getPageSize());
		plAuthAppService.selectPlAuthApp(pageParam,app);
		return EdiResponse.success().data("page",pageParam);
	}
	
	@ApiOperation(value = "应用信息列表", notes = "获取应用信息列表")
    @PostMapping("qryPlAuthAppById")
	public EdiResponse qryPlAuthAppById(@RequestBody PlAuthAppDto dto) {
		
		return EdiResponse.success().data("item",plAuthAppService.getById(dto.getId()));
	}
	
	@ApiOperation(value = "新增应用信息", notes = "新增应用信息")
    @PostMapping("addPlAuthApp")
	public EdiResponse addPlAuthApp(@RequestBody PlAuthAppDto dto) {
		checkField(dto);
		QueryWrapper<PlAuthApp> qw =new QueryWrapper<PlAuthApp>();
		qw.eq("type", dto.getType())
		.eq("appkey",dto.getAppkey())
		.eq("appSecret", dto.getAppsecret())
		.eq("deleteflag","0");
		boolean exists=plAuthAppService.getBaseMapper().exists(qw);
		if(exists) {
			return EdiResponse.failed().returnMgr("应用信息已存在不允许重复创建");
		}
		PlAuthApp app=new PlAuthApp();
		BeanUtils.copyProperties(dto,app);
		app.setAddwho("ris_edi_dev");
		app.setEditwho("ris_edi_dev");
		
		return plAuthAppService.addPlAuthApp(app)?EdiResponse.success():EdiResponse.failed();
	}
	
	@ApiOperation(value = "修改应用信息", notes = "修改应用信息")
    @PostMapping("updPlAuthApp")
	public EdiResponse updPlAuthApp(@RequestBody PlAuthAppDto dto) {
		checkField(dto);
		QueryWrapper<PlAuthShopApp> qw=new QueryWrapper<PlAuthShopApp>();
		qw.eq("appid", dto.getId());
		qw.eq("active", "1");
		boolean flag=plAuthShopAppService.getBaseMapper().exists(qw);
		if(flag) {
			return EdiResponse.failed().returnMgr("绑定表中已经存在且被启用 不允许修改");
		}
		PlAuthApp app=new PlAuthApp();
		BeanUtils.copyProperties(dto,app);
		app.setEditwho("ris_edi_dev");
		app.setEditdate(LocalDate.now());
		
		return plAuthAppService.updateById(app)?EdiResponse.success():EdiResponse.failed();
		
	}
	
	@ApiOperation(value = "批量删除应用信息", notes = "批量删除应用信息")
    @PostMapping("delPlAuthApps")
	public EdiResponse delPlAuthApps(@RequestBody List<PlAuthAppDto> list) {
		try {
			
			// plAuthAppService.delPlAuthApps(list);
			// 逻辑删除 
			for (PlAuthAppDto dto : list) {
				QueryWrapper<PlAuthShopApp> qw=new QueryWrapper<PlAuthShopApp>();
				qw.eq("appid", dto.getId());
				qw.eq("active", "1");
				boolean flag=plAuthShopAppService.getBaseMapper().exists(qw);
				if(flag) {
					return EdiResponse.failed().returnMgr("绑定表中已经存在且被启用 不允许修改");
				}
				PlAuthApp app=new PlAuthApp();
				BeanUtils.copyProperties(dto,app);
				app.setEditwho("ris_edi_dev");
				app.setEditdate(LocalDate.now());
				app.setDeleteflag("1");
				plAuthAppService.updateById(app);
			}
			return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}

	}
	
	@ApiOperation(value = "删除应用信息", notes = "删除应用信息")
    @PostMapping("delPlAuthAppById")
	public EdiResponse delPlAuthAppById(@RequestBody PlAuthAppDto dto) {
		
		return plAuthAppService.removeById(dto.getId())?EdiResponse.success():EdiResponse.failed();
	}
	
	
	private void checkField(PlAuthAppDto dto) {
		 Assert.notNull(dto.getName(),"应用名称不能为空");
		 Assert.notNull(dto.getType(),"应用所属平台不能为空");
		 Assert.notNull(dto.getAppkey(),"应用id不能为空");
		 Assert.notNull(dto.getAppsecret(),"应用密钥不能为空");
		 Assert.notNull(dto.getAuthurl(),"授权地址不能为空");
		 if("淘宝".equals(dto.getType())) {
			 Assert.notNull(dto.getRedirecturl(),"回调地址不能为空");
		 }
		 
	}
	
	

}
